package com.yunban.project.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.article.mapper.ArticleMapper;
import com.yunban.project.article.model.domain.Article;
import com.yunban.project.article.model.dto.ArticleReq;
import com.yunban.project.article.model.dto.ArticleSearchReq;
import com.yunban.project.article.model.vo.ArticleResp;
import com.yunban.project.article.service.ArticleService;
import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.sa.admin.module.system.employee.service.EmployeeService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Resource
    private EmployeeService employeeService;

    @Override
    public List<Article> findByUserId(long userId) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Article::getCreatedBy, userId);
        return this.list(wrapper);
    }

    @Override
    public Long saveArticle(ArticleReq req) {
        Article article = new Article();
        BeanUtils.copyProperties(req, article);
        this.save(article);
        return article.getId();
    }

    @Override
    public ArticleResp getArticleDetail(long id) {
        return SmartPageUtil.convert(this.getById(id), ArticleResp.class);
    }

    @Override
    public PageResult<ArticleResp> getArticleList(ArticleSearchReq req) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(req.getName())) {
            wrapper.like(Article::getName, req.getName());
        }
        // 只查询自己的数据和公开的数据
        // 获取用户id
        Long userId = SmartRequestUtil.getRequestUserId();
        wrapper.eq(Article::getCreatedBy, userId).or().eq(Article::isPublished, true);
        // 分页查询
        Page<Article> paged = this.page(SmartPageUtil.convert2PageQuery(req), wrapper);
        PageResult<ArticleResp> r = SmartPageUtil.convert2PageResult(paged, ArticleResp.class);
        // 获取所有的用户信息
        List<EmployeeEntity> employeeList = this.employeeService.list();
        // 使用stream将employeeList转为map
        Map<Long, EmployeeEntity> employeeMap = employeeList.stream().collect(Collectors.toMap(EmployeeEntity::getEmployeeId, Function.identity()));
        r.getList().forEach(items -> {
            EmployeeEntity employee = employeeMap.get(items.getCreatedBy());
            if (!ObjectUtils.isEmpty(employee)) {
                items.setCreatedByName(employee.getActualName());
            }
        });
        return r;
    }

    Article checkData(long id) {
        Article article = this.getById(id);
        if (ObjectUtils.isEmpty(article)) {
            throw new BusinessException("数据不存在");
        }
        // 判断是否是自己的数据
        if (article.getCreatedBy() != SmartRequestUtil.getUserId()) {
            throw new BusinessException("无权操作");
        }
        return article;
    }

    @Override
    public Boolean publishArticle(long id) {
        Article article = this.checkData(id);
        article.setPublished(true);
        return this.updateById(article);
    }

    @Override
    public Boolean cancelPublishArticle(long id) {
        Article article = this.checkData(id);
        article.setPublished(false);
        return this.updateById(article);
    }
}
